package com.sgcc.lms.openstack.service.impl;

import com.sgcc.lms.openstack.exception.*;
import com.sgcc.lms.openstack.service.CinderService;
import com.sgcc.lms.openstack.util.OpenStackClientFactory;
import com.sgcc.lms.openstack.util.RestClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.openstack4j.api.OSClient;
import org.openstack4j.model.common.ActionResponse;
import org.openstack4j.model.storage.block.Volume;
import org.openstack4j.model.storage.block.VolumeType;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * @Author: 李孟帅
 * @CreateTime: 2020-07-22 17:23
 * @Description:
 */
@Slf4j
@Service
public class CinderServiceImpl implements CinderService {

    private static final String LINE = "==========================================》{}";

    /**
     * @Author 李孟帅
     * @Description 列出卷类型
     * @Date 2020/7/22
     **/
    @Override
    public List<? extends VolumeType> volumeTypes(String tokenId) throws NullTokenException {
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        List<? extends VolumeType> volumeTypes = openStackClient.blockStorage().volumes().listVolumeTypes();
        return volumeTypes;
    }

    /**
     * @Author 李孟帅
     * @Description 列出当前项目有权访问的卷
     * @Date 2020/7/22
     **/
    @Override
    public List<? extends Volume> volumes(String tokenId) throws NullTokenException {
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        List<? extends Volume> volumes = openStackClient.blockStorage().volumes().list();
        return volumes;
    }

    /**
     * @Author 李孟帅
     * @Description 获取卷
     * @Date 2020/7/22
     **/
    @Override
    public Volume getVolume(String tokenId, String id) throws NullTokenException {
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        Volume volume = openStackClient.blockStorage().volumes().get(id);
        return volume;
    }

    /**
     * @Author 李孟帅
     * @Description 创建卷
     * @Date 2020/7/22
     **/
    @Override
    public Volume createVolume(String tokenId, Volume volume) throws NullTokenException {
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        Volume volume1 = openStackClient.blockStorage().volumes().create(volume);
        return volume1;
    }

    /**
     * @Author 李孟帅
     * @Description 更新卷
     * @Date 2020/7/22
     **/
    @Override
    public String updateVolume(String tokenId, Volume volume) throws NullTokenException, IOException, HttpException {
        String id = volume.getId();
        String name = volume.getName();
        String description = volume.getDescription();
        String projectId = OpenStackClientFactory.getProjectId(tokenId);
        String req = "{\n" +
                "    \"volume\": {\n" +
                "        \"name\":\"" + name + "\",\n" +
                "        \"description\": \"" + description + "\"\n" +
                "    }\n" +
                "}";
        StringEntity entity = new StringEntity(req, "UTF-8");
        //构建post请求
        HttpPut httpPut = new HttpPut("http://192.100.5.222:8776/v3/" + projectId + "/volumes/" + id);
        //设置请求头
        httpPut.setHeader("X-Auth-Token", tokenId);
        httpPut.setHeader("Content-Type", "application/json");
        //设置请求体
        httpPut.setEntity(entity);
        //发送http请求
        String result = RestClientUtil.execute(httpPut);
        return result;
    }

    /**
     * @Author 李孟帅
     * @Description 删除卷
     * @Date 2020/7/22
     **/
    @Override
    public ActionResponse deleteVolume(String tokenId, String id) throws NullTokenException {
        OSClient.OSClientV3 openStackClient = OpenStackClientFactory.getOpenStackClient(tokenId);
        ActionResponse delete = openStackClient.blockStorage().volumes().delete(id);
        return delete;
    }
}
